{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.Tensor([2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0.0900, 0.2447, 0.6652])\n",
      "tensor([0.0900, 0.2447, 0.6652])\n"
     ]
    }
   ],
   "source": [
    "#使用pytorch内置的softmax函数\n",
    "softmax = a.softmax(dim=0)\n",
    "print(softmax)\n",
    "#根据公式计算\n",
    "manual_softmax = torch.pow(math.e,a)/torch.pow(math.e,a).sum()\n",
    "print(manual_softmax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([-2.4076, -1.4076, -0.4076])\n",
      "tensor([-2.4076, -1.4076, -0.4076])\n"
     ]
    }
   ],
   "source": [
    "#使用pytorch内置的softmax函数log_softmax\n",
    "log_softmax = a.log_softmax(dim=0)\n",
    "print(log_softmax)\n",
    "#根据公式计算\n",
    "manual_log_softmax = (torch.pow(math.e,a)/torch.pow(math.e,a).sum()).log()\n",
    "print(manual_log_softmax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-2.4076],\n",
      "        [-1.4076],\n",
      "        [-0.4076]])\n",
      "tensor([2.4076, 1.4076, 0.4076])\n",
      "nll_loss:tensor([-4.5200, -3.4200,  0.4076])\n"
     ]
    }
   ],
   "source": [
    "print(manual_log_softmax.unsqueeze(1))\n",
    "#使用pytorch内置的nll_loss计算损失,reduction默认为mean，这里为了演示效果设置未能none\n",
    "#LongTensor[0,0,0]表示正确类别所在索引，这里只有一个元素，方便演示，所以设置成了0\n",
    "nll_loss = F.nll_loss(manual_log_softmax.unsqueeze(1),torch.LongTensor([0,0,0]),reduction='none')\n",
    "#手动计算nll_loss\n",
    "print(nll_loss)\n",
    "#如果是多类别的\n",
    "tmp = torch.Tensor([[-2.4076,4.52],\n",
    "        [-1.4076,3.42],\n",
    "        [-0.4076,0.123]])\n",
    "tmp_index = torch.LongTensor([1,1,0])\n",
    "print(\"nll_loss:{}\".format(F.nll_loss(tmp,tmp_index,reduction='none')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(1.5121)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#交叉熵损失函数\n",
    "x = torch.Tensor([[1,2],[0.5,3],[0.9,4]])\n",
    "label = torch.LongTensor([0,1,0])\n",
    "loss = nn.CrossEntropyLoss()\n",
    "loss(x,label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2689, 0.7311],\n",
       "        [0.0759, 0.9241],\n",
       "        [0.0431, 0.9569]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "softmax = x.softmax(dim=1)\n",
    "softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.3133, -0.3133],\n",
       "        [-2.5789, -0.0789],\n",
       "        [-3.1441, -0.0441]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log = softmax.log()\n",
    "log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.3133, -0.3133],\n",
       "        [-2.5789, -0.0789],\n",
       "        [-3.1441, -0.0441]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_softmax = x.log_softmax(dim=1)\n",
    "log_softmax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(1.5121)"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.nll_loss(x.log_softmax(dim=1),label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
